package com.jldx.oa.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jldx.oa.bean.SaleOrder;

public class ReportController extends BaseController{

	public void report(String path){
		render("/WEB-INF/views/report/"+path);
	}
	
	public void goods(){
		String name = getPara("name");
		setAttr("name", name);
		SqlPara sql = Db.getSqlPara("report.goods",Kv.by("name", name));
		List<Record> rs = Db.find(sql);
		
		for(Record r : rs){
			r.set("state_cn", SaleOrder.stateMap.get(r.get("state")));
		}
		setAttr("cs", rs);
		report("goods.html");
	}
	
	public void dayIncome(){
		Date startDate = getParaToDate("startDate");
		Date endDate = getParaToDate("endDate");
		setAttr("startDate", getPara("startDate"));
		setAttr("endDate", getPara("endDate"));
		
		if(startDate==null){
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, 0);
			c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
			startDate = c.getTime();
		}
		
		if(endDate==null){
			Calendar  c = Calendar.getInstance();
			c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));  
			endDate = c.getTime();
		}
		
		String sql = Db.getSql("report.dayIncome");
		List<Record> rs = Db.find(sql,startDate,endDate);
		setAttr("cs", rs);
		report("day_income.html");
	}
	
	public void asset(){
		String sql = Db.getSql("report.asset");
		List<Record> rs = Db.find(sql);
		
		Record re = new Record();
		re.set("category_name", "合计");
		re.set("name", "--");
		re.set("store_count", 0);
		re.set("price", 0);
		re.set("store_amount", 0);
		for(Record r : rs){
			re.set("store_count",re.getFloat("store_count") +r.getFloat("store_count"));
			re.set("price",re.getFloat("price") +r.getFloat("price"));
			Float sa1 = re.getFloat("store_amount");
			Float sm2 = r.getFloat("store_amount");
			if(sa1==null){sa1=0f;}
			if(sm2==null){sm2=0f;}
			re.set("store_amount",sa1 +sm2);
		}
		rs.add(re);
		setAttr("cs", rs);
		report("asset.html");
	}
	
}
